﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Nuwanda.Common;
using Nuwanda.TfsSpotlight.Data;
using StructureMap;

namespace Nuwanda.TfsSpotlight.Views.Options
{
    [Author("Bartłomiej Legiędź")]
    [Pluggable("OptionsView")]
    public partial class OptionsView : BaseUserControl, IOptionsView
    {
        private IOptionsPresenter _presener;

        public OptionsView()
        {
            InitializeComponent();
            this.ilImages.Images.Add(Properties.Resources.Servers);
        }

        public void AttachPresenter(IOptionsPresenter presenter)
        {
            this._presener = presenter;
        }

        public ApplicationSettings DataSource
        {
            set { SetDataSource(value); }
        }

        public List<ServerSettingsItem> ServersDataSource
        {
            set { SetServersDataSource(value); }
        }

        private void SetServersDataSource(List<ServerSettingsItem> dataSource)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => this.SetServersDataSource(dataSource)));
                return;
            }

            this.bsServers.DataSource = dataSource;
        }

        private void SetDataSource(ApplicationSettings dataSource)
        {
            if(this.InvokeRequired)
            {
                this.Invoke(new Action(() => this.SetDataSource(dataSource)));
                return;
            }

            this.bsSettings.DataSource = dataSource;
        }

        private void HandleAddServer(object sender, EventArgs e)
        {
            this._presener.HandleAddServer();
        }

        private void HandleDiscoverServers(object sender, EventArgs e)
        {
            this._presener.HandleDiscoverServers();
        }

        private void HandleDeleteServer(object sender, EventArgs e)
        {
            this._presener.HandleDeleteServer();
        }

        public void SelectServer(ServerSettingsItem server)
        {
            if (this.lbServers.InvokeRequired)
            {
                this.Invoke(new Action(() => this.SelectServer(server)));
                return;
            }

            if (server == null) return;
            
            this.lbServers.SelectedItem = server;
        }

        public ServerSettingsItem GetSelectedServer()
        {
            if (this.lbServers.InvokeRequired)
            {
                return this.Invoke(new Func<ServerSettingsItem>(() => this.GetSelectedServer())) as ServerSettingsItem;
            }

            return this.lbServers.SelectedItem as ServerSettingsItem;
        }

        public void Disable()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(this.Disable));
                return;
            }

            this.Enabled = false;
        }

        public void Enable()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(this.Enable));
                return;
            }

            this.Enabled = true;
        }

        public void RefreshSelectedItem()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(this.RefreshSelectedItem));
                return;
            }

            int index = this.lbServers.SelectedIndex;
            if (index < 0) return;

            this.lbServers.Invalidate(this.lbServers.GetItemRectangle(index));
        }
    }
}